<template>
	<view>
		<view class="ten-line"></view>
		<!-- 头像 -->
		<button class="head-view" type="primary" @click="getHeadImg" style="background-color:transparent;background:transparent;border-color: transparent;" >
			<text class="head-txt">头像</text>
			<image class="head-img" :src="headImage" mode="scaleToFill"></image>
		</button>
		<view class="line"></view>
		<!-- 名字 -->
		<view class="item-view" @click="nameClick">
			<text class="item-txt">名字</text>
			<text class="item-desc">{{userData.surname+userData.name}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<!-- <view class="line"></view> -->
		<!-- 拾亲号 -->
	<!-- 	<view class="item-view">
			<text class="item-txt">拾亲号</text>
			<text v-if="userData.username" class="item-desc">{{userData.username}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"></view> -->
		<!-- 我的二维码 -->
	<!-- 	<view class="item-view">
			<text class="item-txt">我的二维码</text>
			<image class="my-code" src="../../static/info-my-code.png"></image>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view> -->
		<view class="ten-line"></view>
		<!-- 性别 -->
		<view class="item-view">
			<text class="item-txt">性别</text>
			<text class="item-desc">{{userData.gender=='0'?'女':'男'}}</text>
			<image class="item-arrow" ></image>
		</view>
		<view class="line"></view>
		<!-- 生日 -->
		<view class="item-view" @click="timeClick" data-item="1">
			<text class="item-txt">生日</text>
			<text class="item-desc">{{userData.birthday2==1?userData.birthday1:userData.birthday}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"></view>
		<!-- 结婚纪念日 -->
		<view class="item-view" @click="timeClick" data-item="2">
			<text class="item-txt">结婚纪念日</text>
			<text class="item-desc" >{{userData.wedding2==1?userData.wedding1:userData.wedding}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<!-- <view class="line"></view> -->
		<!-- 字号 -->
	<!-- 	<view class="item-view">
			<text class="item-txt">字号</text>
			<text v-if="userData.card_zi"  class="item-desc" >{{userData.card_zi}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view> -->
		<!-- <view class="line"></view> -->
		<!-- 备注 -->
		<!-- <view class="item-view">
			<text class="item-txt">备注</text>
			<text v-if="userData.card_remark"  class="item-desc" >{{userData.card_remark}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view> -->
		<view class="ten-line"></view>
		<!-- 祖籍地 -->
		<view class="item-view" @click="addressClick" data-item="4">
			<text class="item-txt">祖籍地</text>
			<text class="item-desc">{{userData.ancestral}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"></view>
		<!-- 现居地 -->
		<view class="item-view" @click="addressClick" data-item="5">
			<text class="item-txt">现居地</text>
			<text class="item-desc">{{userData.ancepresent}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"></view>
		<!-- 签名 -->
		<!-- <view class="item-view" @click="descClick">
			<text class="item-txt">签名</text>
			<text v-if="userData.signstr" class="item-desc" style="width: 500rpx;text-align: right;">{{userData.signstr}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"></view> -->
		<!-- 个人简介 -->
		<view class="item-view" @click="bioClick">
			<text class="item-txt">个人简介</text>
			<text v-if="userData.bio" class="item-desc" style="width: 400rpx;text-align: right;">{{userData.bio}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="ten-line" v-if="!userData.PQD"/>
		<view class="item-view" v-if="!userData.PQD">
			<text class="item-txt">在世</text>
			<text class="item-flex-view"></text>
			<switch :disabled="isAuth=='0'" class="switch-btn"  @change="switchChange" :checked="userData.is_living==1" color="#FF4206" />
		</view>
		<view class="line" v-if="userData.is_living==0&&!userData.PQD"/>
		<!-- 祭日 -->
		<view class="item-view" @click="timeClick" v-if="userData.is_living==0&&!userData.PQD" data-item="3">
			<text class="item-txt">祭日</text>
			<text class="item-desc" style="width: 400rpx;text-align: right;">
			{{userData.dieday2==1?userData.dieday1:userData.dieday}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"  v-if="userData.is_living==0&&!userData.PQD"/>
		<!-- 墓地 -->
		<view class="item-view" @click="addressClick" v-if="userData.is_living==0&&!userData.PQD"  data-item="6">
			<text class="item-txt">墓地</text>
			<text class="item-desc" style="width: 400rpx;text-align: right;">{{userData.ancepresent}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="ten-line" v-if="!userData.PQD"/>
		<changeNamePop :isShow="isChangeNameShow" :dataSource="nameData" @returnInputDialog="inputDialogReturn"/>
		<choseAddress :isShow="isChoseAddressShow" @returnAddress="addressReturn"/>
		<inputDesc :isShow="isInputDescShow" :dataSource="userData.signstr" @returnInputDesc="inputDescReturn"/>
		<inputBio :isShow="isInputBioShow" :dataSource="userData.bio"  @returnInputBio="inputBioReturn"/>
		<headChoosePop :isShow="isShowChooseHeadPop" :dataSource="userData.gender"  @returnHeadChoose="returnHeadChoose"/>
		<contactAdminPop v-if="isShowcontactAdminPop" @returnContactAdminPop="returnContactAdminPop" />
	</view>
</template>

<script>
	import request from '../index/request.js';
	import changeNamePop from "@/components/change-name-pop/change-name-pop.vue";
	import choseAddress from '../../components/chose-address/chose-address.vue'
	import inputDesc from "@/components/input-desc/input-desc.vue";
	import inputBio from "@/components/input-bio/input-bio.vue";
	import headChoosePop from "@/components/head-choose-pop/head-choose-pop.vue";
	import contactAdminPop from "@/components/contact-admin-pop/contact-admin-pop.vue";
	
	export default {
		components: {
			changeNamePop,choseAddress,inputDesc,inputBio,headChoosePop,contactAdminPop
		},
		data() {
			return {
				headImage:"",
				userData:null,
				isChangeNameShow:false,
				isChoseAddressShow:false,
				isInputDescShow:false,
				isInputBioShow:false,
				addressType:"0",
				timeType:"0",
				isShowChooseHeadPop:false,
				isShowcontactAdminPop:false,
				nameData:{
				},
				cardId:"",
				isAuth:"0"			,
			}
		},
		onUnload:function(){
			uni.$off("selectTimeReturn")
		},
		methods: {
			onShow:function(){
				this.share.url= '/pages/tabBar/home/index?appInviteId='+this.appUtil.getAppInviteId()
				this.share.shareType=1
			},
			onLoad: function(options) {
				var that=this
				this.appUtil.saveAppInviteId(options)
				this.cardId=options.cardId
				this.binding=options.binding
				this.getUserInfo()
				uni.$on("selectTimeReturn",(data)=>{
					that.timeReturn(data)
				})
			},
			returnContactAdminPop:function(e){
				this.isShowcontactAdminPop=e.isclose
				if(e.tag){
					uni.navigateTo({
						url:"../manager-member/index"
					})
				}
			},
			returnHeadChoose:function(e){
				this.isShowChooseHeadPop=e.isclose
				if(e.tag){
					if(e.position!=0){
						this.modifyUserInfo("head",e.position)
					}else{
						var that = this
						uni.chooseImage({
							count:1,// 最多可以选择的图片张数，默认9
							sizeType: ['original', 'compressed'],//original 原图，compressed 压缩图，默认二者都有
							sourceType: ['album', 'camera'],//album 从相册选图，camera 使用相机，默认二者都有。如需直接开相机或直接选相册，请只使用一个选项
							success: function (res) {
								uni.uploadFile({
									url: "https://a.shiqin.wang/addons/alioss/index/upload",
									filePath: res.tempFilePaths[0],
									name: 'file',
									success: function (result) {
										if(result.statusCode==200){
											let headData=JSON.parse(decodeURI(result.data))
											that.userData.head=headData.data.url_s
											that.headImage=headData.data.url_s
											that.modifyUserInfo("head",headData.data.url)
										}
									}
								})
							}
						});
					}
				}
			},
			getHeadImg:function(){
				if (this.isAuth == '0') {
					this.isShowcontactAdminPop=true
					return
				}
				this.isShowChooseHeadPop=true
			},
			nameClick:function(){
				if (this.isAuth == '0') {
					this.isShowcontactAdminPop=true
					return
				}
				this.nameData.nickname=this.userData.nickname
				this.nameData.nickcname=this.userData.nickcname
				this.isChangeNameShow=true
			},
			inputDialogReturn:function(event){
				this.isChangeNameShow=event.isclose
				let data = JSON.parse(JSON.stringify(this.userData));
				if(event.tag==='sure'){
					data.nickname=event.inputXingKey
					data.nickcname=event.inputMingKey
					this.userData=data
					this.modifyUserInfo("names",data.nickname+"-"+data.nickcname)
				}
			},
			timeClick:function(event){
				if (this.isAuth == '0') {
					this.isShowcontactAdminPop=true
					return
				}
				var that = this
				this.timeType = event.currentTarget.dataset.item;
				uni.navigateTo({
					url:"../choose-time/index?isShowInput=0"
				})
			},
			timeReturn:function(e){
				let data = JSON.parse(JSON.stringify(this.userData));
				if (e.startTime) {
					switch (this.timeType) {
						case "1":
							//需要展示的时间 
							if (e.timeType == 0) {
								this.modifyUserInfoData("birthday", e.startTime, e.dateType)
							} else {
								this.modifyUserInfo("birthday1", e.startTime)
							}
							break;
						case "2":
							if (e.timeType == 0) {
								this.modifyUserInfoData("wedding", e.startTime, e.dateType)
							} else {
								this.modifyUserInfo("wedding1", e.startTime)
							}
							break;
						case "3":
							if (e.timeType == 0) {
								this.modifyUserInfoData("dieday", e.startTime, e.dateType)
							} else {
								this.modifyUserInfo("dieday1", e.startTime)
							}
							break;
					}
				} else {
					switch (this.timeType) {
						case "1":
							if (e.timeType == 0) {
								this.modifyUserInfoData("birthday", e.startTime, e.dateType)
							} else {
								this.modifyUserInfo("birthday1", e.startTime)
							}
							break;
						case "2":
							if (e.timeType == 0) {
								this.modifyUserInfoData("wedding", e.startTime, e.dateType)
							} else {
								this.modifyUserInfo("wedding1", e.startTime)
							}
							break;
						case "3":
							if (e.timeType == 0) {
								this.modifyUserInfoData("dieday", e.startTime, e.dateType)
							} else {
								this.modifyUserInfo("dieday1", e.startTime)
							}
							break;
					}
				}
				this.userData=data
			},
			addressClick:function(event){
				if (this.isAuth == '0') {
					this.isShowcontactAdminPop=true
					return
				}
				this.isChoseAddressShow=true
				this.addressType = event.currentTarget.dataset.item;
			},
			addressReturn:function(e){
				this.isChoseAddressShow=e.isclose
				if (e.tag) {
					let data = JSON.parse(JSON.stringify(this.userData));
					if (e.address) {
						switch (this.addressType) {
							case "4": //祖籍地
								data.ancestral = e.address
								this.modifyUserInfo("ancestral", e.address)
								break;
							case "5": //现居地
								data.ancepresent = e.address
								this.modifyUserInfo("ancepresent", e.address)
								break;
							case "6": //墓地
								data.ancepresent = e.address
								this.modifyUserInfo("ancepresent", e.address)
								break;
						}
					} else {
						switch (this.addressType) {
							case "4": //祖籍地
								data.ancestral = e.address
								this.modifyUserInfo("ancestral", "")
								break;
							case "5": //现居地
								data.ancepresent = e.address
								this.modifyUserInfo("ancepresent", "")
								break;
							case "6": //墓地
								data.ancepresent = e.address
								this.modifyUserInfo("ancepresent", "")
								break;
						}
					}
					this.userData=data
				}
			},
			descClick:function(){
				if (this.isAuth == '0') {
					this.isShowcontactAdminPop=true
					return
				}
				this.isInputDescShow=true
			},
			inputDescReturn:function(event){
				console.log(JSON.stringify(event))
				this.isInputDescShow=event.isclose
				let data = JSON.parse(JSON.stringify(this.userData));
				if(event.inputStr){
					this.modifyUserInfo("signstr",event.inputStr)
					data.signstr=event.inputStr
					this.userData=data
				}
			},
			bioClick:function(){
				if (this.isAuth == '0') {
					this.isShowcontactAdminPop=true
					return
				}
				this.isInputBioShow=true
			},
			inputBioReturn:function(event){
				console.log(JSON.stringify(event))
				this.isInputBioShow=event.isclose
				let data = JSON.parse(JSON.stringify(this.userData));
				if(event.inputStr){
					this.modifyUserInfo("bio",event.inputStr)
					data.bio=event.inputStr
					this.userData=data
				}
			},
			switchChange:function(event){
				if (this.isAuth == '0') {
					this.isShowcontactAdminPop=true
					return
				}
				let data = JSON.parse(JSON.stringify(this.userData));
				if(event.target.value){
					data.is_living=1
				}else{
					data.is_living=0
				}
				this.userData=data
				this.modifyUserInfo("is_living",event.target.value?"1":"0")
			},
			getUserInfo:function(){
				uni.showToast({
					icon:"loading"
				})
				var that = this
				request.post("api/apiv2/GetTreeCardInfo", {
					CardId: this.cardId
				}).then(data => {
					that.userData=data
					that.headImage=data.head
					that.isAuth=data.auth
				})
			},
			modifyUserInfo:function(key,value){
				console.log("key:"+key+",value:"+value)
				var that = this
				if (!this.cardId) {
					return
				}
				request.post("api/apiv2/SetTreeCardInfo", {
					CardId: this.cardId,
					SetKey: key,
					SetVal: value,
				}).then(data => {
					that.getUserInfo()
					uni.$emit("refrashTree")
					uni.showToast({
						title: "修改成功",
						duration: 1000,
						icon: "none"
					})
				})
			},
			modifyUserInfoData:function(key,value,time){
				console.log("key:"+key+",value:"+value+",time:"+time)
				var that = this
				if (!this.cardId) {
					return
				}
				request.post("api/apiv2/SetTreeCardInfo", {
					CardId: this.cardId,
					SetKey: key,
					SetVal: value,
					is_lunar: time
				}).then(data => {
					if (key == "birthday" && !value) {
						this.modifyUserInfo("birthday1", "")
					} else if (key == "wedding" && !value) {
						this.modifyUserInfo("wedding1", "")
					} else if (key == "dieday" && !value) {
						this.modifyUserInfo("dieday1", "")
					} else {
						that.getUserInfo()
						uni.$emit("refrashTree")
						uni.showToast({
							title: "修改成功",
							duration: 1000,
							icon: "none"
						})
					}
				})
			},
		}
	}
</script>

<style>
	@import "index.css";
</style>
